/* 
** test struct 
*/
#include <stdio.h>
#include <string.h>

static char * lcs_logname = "lcs_struct";
static char lcs_logbuf[512];
static void lcs_testlog()
{
	int fp;
	
	fp = fopen( lcs_logname, "at" );
	if( !fp ) return;

	printf( "%s", lcs_logbuf );
	fprintf( fp, "%s", lcs_logbuf );
	
	fclose( fp );
}

/*
** test begin...
*/
struct st
{
	int a, b;
	struct ss
	{
		int a;
		int b;
	}s;
};


int struct_test1()
{
	struct st a, *p;

	p = &a;

	p->a = 1;
	p->b = 2;
	p->s.a = 3;
	p->s.b = 4;

	sprintf( lcs_logbuf, "%d %d %d %d", a.a, a.b, a.s.a, a.s.b );
	lcs_testlog();
	return 0;
}

int struct_test2()
{
	struct st a, *p;

	p = &a;

	a.a = 1;
	a.b = 2;
	a.s.a = 3;
	a.s.b = 4;

	sprintf( lcs_logbuf, "%d %d %d %d", p->a, p->b, p->s.a, p->s.b );
	lcs_testlog();
	return 0;
}

int struct_test3()
{
	int i;
	struct st a[3];
	struct st *p = a;

	for( i=0; i<3; i++ ){
		a[i].a =   i*100 +1;
		a[i].b =   i*100 +2;
		a[i].s.a = i*100 +3;
		a[i].s.b = i*100 +4;
	}

	for( i=0; i<3; i++ ){
		sprintf( lcs_logbuf, "%03d %03d %03d %03d ", p[i].a, p[i].b, p[i].s.a, p[i].s.b );
		lcs_testlog();
	}

	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	for( i=0; i<3; i++ ){
		sprintf( lcs_logbuf, "%03d %03d %03d %03d ", p->a, p->b, p->s.a, p->s.b );
		lcs_testlog();
		p++;
	}
	p = a;

	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	for( i=0; i<3; i++ ){
		sprintf( lcs_logbuf, "%03d %03d %03d %03d ", (p+i)->a, (p+i)->b, (p+i)->s.a, (p+i)->s.b );
		lcs_testlog();
	}
	return 0;
}

int struct_test4()
{
	int i;
	struct st a[3];
	struct st *p = a;

	for( i=0; i<3; i++ ){
		p->a =   i*100 +1;
		p->b =   i*100 +2;
		p->s.a = i*100 +3;
		p->s.b = i*100 +4;
		p++;
	}
	p = a;

	for( i=0; i<3; i++ ){
		sprintf( lcs_logbuf, "%03d %03d %03d %03d ", p[i].a, p[i].b, p[i].s.a, p[i].s.b );
		lcs_testlog();
	}
	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	for( i=0; i<3; i++ ){
		sprintf( lcs_logbuf, "%03d %03d %03d %03d ", p->a, p->b, p->s.a, p->s.b );
		lcs_testlog();
		p++;
	}
	return 0;
}

int lcs_struct()
{
	struct_test1();
	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	struct_test2();
	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	struct_test3();
	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	struct_test4();
	sprintf( lcs_logbuf, "\n" );
	lcs_testlog();
	return 0;
}
